<?php
if (!isset($_SESSION['logged']))
    print "<script language='javascript'>
        window.location.href='./?url=404';
        </script>";
else {
?>
<script type="text/javascript" Language="JavaScript">
    <!--
    function Blank_TextField_Validator2()
    {
        // Check the value of the element
        // from the form
        if (document.postform.title.value == ""){
            // If null display and alert box
            alert("Please fill in the title field.");
            // Place the cursor on the field for revision
            document.postform.title.focus();
            // return false to stop further processing
            return (false);
        }
        // If text_name is not null continue processing
        return (true);
    }
    -->
</script>
<div id="mainCol" class="clearfix">
    <h3 id="why">Post a new file...</h3>
    <ul id="maincon">
        <li class="clearfix last">
            <fieldset id="forms">
            <table width="100%">
                <form method="post" name="postform" action="?url=posting" onsubmit="return Blank_TextField_Validator2()">
                    <tr>
                        <td valign="top" width="25%"><b>Title:</b></td>
                        <td><input type="text" maxlength="200" name="title" id="f_input" size="55" /></td>
                    </tr>
                    <tr>
                        <td valign="top" width="25%"><b>Type:</b></td>
                        <td>
<?
    $sql = "SELECT * FROM `type`;";
    $result = mysql_query($sql, $link);
    $row = mysql_fetch_assoc($result);
?>
                            <input type="radio" name="type" checked value="<?= $row['id']; ?>"/> <?= $row['title']; ?><br />
<?
    while ($row = mysql_fetch_assoc($result)) {
?>
                            <input type="radio" name="type" value="<?= $row['id']; ?>"/> <?= $row['title']; ?><br />
<?
    }
?>
                        </td>
                    </tr>
                    <tr>
                        <td valign="top" width="25%"><b>Description:</b></td>
                        <td><textarea name="description" style="width: 99%" rows="7"></textarea></td>
                    </tr>
                    <tr>
                        <td valign="top" width="25%"><b>Link:</b></td>
                        <td><input type="text" name="link" id="f_input" size="55" /></td>
                    </tr>
                    <tr>
                        <td colspan="2"><br><input type="submit" id="f_submit" value="Post" /></td>
                    </tr>
                </form>
            </table>
            </fieldset>
        </li>
    </ul>
<?
    $iduser = $_SESSION['id'];
    $sql = "SELECT `f`.*, `t`.`title` as `type`
            FROM `file` `f`, `user_file` `uf`, `type` `t`
            WHERE `uf`.`user_id` = '$iduser'
                AND `uf`.`file_id` = `f`.`id`
                AND `f`.`type_id` = `t`.`id`
            ORDER BY `f`.`id` DESC;";
    $result = mysql_query($sql, $link);
    if(mysql_num_rows($result)>0) {
?>
    <h3 id="why">My files:</h3>
    <ul id="maincon">
        <li class="clearfix last">
            <table width="100%">
<?
        while($row = mysql_fetch_assoc($result)){
            $filetype = strtolower($row['type']);
?>
                <tr>
                    <td width="32px">
                        <a href="?url=file&file=<?= $row['id']; ?>"><img src="images/<?= $filetype; ?>.png" title="<?= $row['title']; ?>" width="24px" height="24px" alt="<?= $row['type']; ?>" class="image" /></a>
                    </td>
                    <td>
                        &nbsp;<a href="?url=file&file=<?= $row['id']; ?>"><b><?= $row['title']; ?></b></a>
                    </td>
                    <td align="right" width="32px">
                        <a href="?url=fileedit&file=<?= $row['id']; ?>" title="Edit"><img src="images/edit.png" width="24px" height="24px" alt="Edit" class="image" /></a>
                    </td>
                    <td align="right" width="32px">
                        <a href="?url=filedeleting&file=<?= $row['id']; ?>" onclick="return confirm('Are you sure you want to delete this item?');" title="Delete"><img src="images/delete.png" width="24px" height="24px" alt="Delete" class="image" /></a>
                    </td>
                </tr>
<?
        }
?>
            </table>
        </li>
    </ul>
<?
    }
    if($_SESSION['level']=="admin"){
        $sql = "SELECT * FROM `type` ORDER BY `title`;";
        $result = mysql_query($sql, $link);
        if(mysql_num_rows($result)>0){
?>
    <h3 id="why">Administration - File types</h3>
    <ul id="maincon">
        <li class="clearfix last">
            <table width="100%">
<?
            while($row = mysql_fetch_assoc($result)){
                $filetype = strtolower($row['title']);
?>
                <tr>
                    <td width="32px">
                        <img src="images/<?= $filetype; ?>.png" title="<?= $row['title']; ?>" width="24px" height="24px" alt="Type" class="image" />
                    </td>
                    <td><b>&nbsp;<?= $row['title'] ?></b></td>
                    <td align="right" width="32px"><a href="?url=typeedit&type=<?= $row['id']; ?>" title="Edit"><img src="images/edit.png" width="24px" height="24px" alt="Edit" class="image" /></a></td>
                    <td align="right" width="32px"><a href="?url=typedeleting&type=<?= $row['id']; ?>" onclick="return confirm('Are you sure you want to delete this item?');" title="Delete"><img src="images/delete.png" width="24px" height="24px" alt="Delete" class="image" /></a></td>
                </tr>
<?
            }
?>
                <tr><td colspan="3"><br><input class="art-button" type="button" value="Add new type" onclick="window.location.href='?url=typeadd'" /></td></tr>
<?
        }
?>
            </table>
<?
}
?>
        </li>
    </ul>
</div>
<script type="text/javascript" language="javascript">
    document.postform.title.focus();
</script>
<?
}
?>
